package com.lzq.config;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;

@Profile("production")
@Configuration
@PropertySource("classpath:config.properties")
public class ProductionDbConfig {
	
	@Value("${datasource.url}")  
    private String dbUrl;  
      
    @Value("${datasource.username}")  
    private String username;  
      
    @Value("${datasource.password}")  
    private String password;  
      
    @Value("${datasource.driver-class-name}")  
    private String driverClassName;  
      
    @Value("${datasource.initialSize}")  
    private int initialSize;  
      
    @Value("${datasource.minIdle}")  
    private int minIdle;  
      
    @Value("${datasource.maxActive}")  
    private int maxActive;  
      
    @Value("${datasource.maxWait}")  
    private int maxWait;  
      
    @Value("${datasource.timeBetweenEvictionRunsMillis}")  
    private int timeBetweenEvictionRunsMillis;  
      
    @Value("${datasource.minEvictableIdleTimeMillis}")  
    private int minEvictableIdleTimeMillis;  
      
    @Value("${datasource.validationQuery}")  
    private String validationQuery;  
      
    @Value("${datasource.testWhileIdle}")  
    private boolean testWhileIdle;  
      
    @Value("${datasource.testOnBorrow}")  
    private boolean testOnBorrow;  
      
    @Value("${datasource.testOnReturn}")  
    private boolean testOnReturn;  
      
    @Value("${datasource.poolPreparedStatements}")  
    private boolean poolPreparedStatements; 
	
    @Value("${datasource.filters}")  
    private String filters;  
      
    @Value("{datasource.connectionProperties}")  
    private String connectionProperties;  
    
    @Value("${datasource.maxPoolPreparedStatementPerConnectionSize}")  
    private int maxPoolPreparedStatementPerConnectionSize;  
    
	public ProductionDbConfig() {
		System.out.println("@Configuration ProductionConfig created ");
	}

	@Bean
	public PlatformTransactionManager transactionManager() {
		System.out.println(ProductionDbConfig.class + "transactionManager created ");
		DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
		transactionManager.setDataSource(datasource());
		return transactionManager;
	}

	@Bean
	public DataSource datasource() {
		DruidDataSource datasource=new DruidDataSource();
		 datasource.setUrl(this.dbUrl);  
	        datasource.setUsername(username);  
	        datasource.setPassword(password);  
	        datasource.setDriverClassName(driverClassName);  
	        //configuration  
	        datasource.setInitialSize(initialSize);  
	        datasource.setMinIdle(minIdle);  
	        datasource.setMaxActive(maxActive);  
	        datasource.setMaxWait(maxWait);  
	        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);  
	        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);  
	        datasource.setValidationQuery(validationQuery);  
	        datasource.setTestWhileIdle(testWhileIdle);  
	        datasource.setTestOnBorrow(testOnBorrow);  
	        datasource.setTestOnReturn(testOnReturn);  
	        datasource.setPoolPreparedStatements(poolPreparedStatements);  
	        datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);  
	        try {  
	            datasource.setFilters(filters);  
	        } catch (SQLException e) { 
	        	e.printStackTrace();
	        }  
	        datasource.setConnectionProperties(connectionProperties);  
		
		return datasource;
	}


}
